home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS01.ADF
/
C
/
Mandel.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-01-09
|
9KB
|
292 lines
/*********************************************************************
****** ******************
****** Mandel.c - A Mandelbrot Set viewer for Amiga ******************
****** ******************
****** by Kevin A. Bjorke ******************
****** 25724 Salceda Road ******************
****** Valencia, CA 91355 ******************
****** CIS:74756,464 ******************
****** ******************
*********************************************************************/
#include <exec/types.h>
#include <exec/tasks.h>
#include <exec/libraries.h>
#include <exec/devices.h>
#include <devices/keymap.h>
#include <graphics/copper.h>
#include <graphics/display.h>
#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <graphics/view.h>
#include <graphics/gels.h>
#include <graphics/regions.h>
#include <hardware/blit.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <math.h>
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define XPIXSIDE 150 /* Width of Mandel box in pixels */
#define YPIXSIDE 150 /* Height '' '' '' */
#define LIMIT 127 /* max iterations per pixel */
#define XSTART 5 /* start of box in X */
#define YSTART 12 /* '' '' '' Y */
#define XTOP XSTART+XPIXSIDE
#define YTOP YSTART+YPIXSIDE
#define OKAY ((xmp>=XSTART)&&(xmp<XTOP)&&(ymp>=YSTART)&&(ymp<YTOP))
struct GfxBase *GfxBase; /* Export the library pointers */
struct IntuitionBase *IntuitionBase;
struct RastPort *rp; /* Graphics structures */
struct ViewPort *vp;
struct TextAttr TestFont = {
"topaz.font", /* Standard system font */
8, 0, 0
};
struct Window *w; /* Intuition structures */
struct Screen *screen;
struct IntuiMessage *message;
struct NewScreen ns = {
0, 0, /* start position */
320, 200, 4, /* width, height, depth */
0, 1, /* detail pen, block pen */
0, /* Normal ViewMode */
CUSTOMSCREEN, /* screen type */
&TestFont, /* font to use */
" Mandel ", /* default title for screen */
NULL /* pointer to additional gadgets */
};
struct NewWindow nw = {
0, 11, /* start position */
320, 186, /* width, height */
-1, -1, /* detail pen, block pen */
MOUSEBUTTONS|CLOSEWINDOW, /* IDCMP flags */
ACTIVATE|WINDOWCLOSE, /* window flags */
NULL, /* pointer to first user gadget */
NULL, /* pointer to user checkmark */
" Mouse Zooms In ", /* window title */
NULL, /* pointer to screen (set below) */
NULL, /* pointer to superbitmap */
0, 0, 320, 186, /* ignored since not sizeable */
CUSTOMSCREEN /* type of screen desired */
};
double acorn = -2.0; /* real portion - corner */
double bcorn = -1.25; /* complex portion - corner */
double xside = 2.5; /* span of box side in complex space */
double yside = 2.5; /* span of box side in complex space */
double szlim = 2.; /* iterative size limit */
double ac, bc; /* these set by code */
double xgap, ygap; /* Likewise */
int xm = XSTART, ym = YSTART; /* pixel counters */
int filled = FALSE;
/*****************
** Main Program **
*****************/
main()
{
ULONG class;
USHORT code;
int xmp, ymp;
xgap = xside/XPIXSIDE;
ygap = yside/YPIXSIDE;
ac = acorn;
bc = bcorn;
GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
if (GfxBase == NULL)
exit();
IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
if (IntuitionBase == NULL) {
CloseLibrary(GfxBase);
exit();
}
screen = (struct Screen *)OpenScreen(&ns);
if (screen == NULL) {
CloseLibrary(IntuitionBase); CloseLibrary(GfxBase);
exit();
}
nw.Screen = screen; /* Open window in our new screen */
w = (struct Window *)OpenWindow(&nw);
if (w == NULL) {
CloseScreen(screen);
CloseLibrary(IntuitionBase);
CloseLibrary(GfxBase);
exit();
}
vp = &screen->ViewPort; /* Set colors in screen's VP */
rp = w->RPort; /* Render into the window's RP */
/* Set the color registers */
SetRGB4(vp, 0, 00, 00, 00);
SetRGB4(vp, 1, 15, 00, 00);
SetRGB4(vp, 2, 00, 15, 00);
SetRGB4(vp, 3, 00, 00, 15);
SetRGB4(vp, 4, 15, 15, 00);
SetRGB4(vp, 5, 15, 00, 15);
SetRGB4(vp, 6, 00, 15, 15);
SetRGB4(vp, 7, 08, 08, 08);
SetRGB4(vp, 8, 15, 08, 00);
SetRGB4(vp, 9, 00, 08, 15);
SetRGB4(vp,10, 08, 15, 08);
SetRGB4(vp,11, 14, 04, 02);
SetRGB4(vp,12, 04, 11, 01);
SetRGB4(vp,13, 13, 06, 15);
SetRGB4(vp,14, 02, 04, 06);
SetRGB4(vp,15, 15, 15, 15);
SetBPen(rp, 0); /* Insure clean text */
fieldbox();
FOREVER {
while((message = (struct IntuiMessage *)GetMsg(w->UserPort)) != NULL) {
class = message->Class;
code = message->Code;
xmp = w->MouseX;
ymp = w->MouseY;
ReplyMsg(message); /* Can't reply until done using it! */
if(class == CLOSEWINDOW) { /* Exit the program */
CloseWindow(w);
CloseScreen(screen);
CloseLibrary(IntuitionBase);
CloseLibrary(GfxBase);
exit();
};
if(class == MOUSEBUTTONS && code == SELECTDOWN) { /* start over */
if (OKAY) {
span_box(xmp,ymp);
}
}
}
if (!filled)
filled = Mandel();
}
CloseWindow(w);
CloseScreen(screen);
CloseLibrary(IntuitionBase);
CloseLibrary(GfxBase);
}
Mandel()
{
SHORT c;
c = Mandcolor(ac,bc);
SetAPen(rp,c);
WritePixel(rp,xm,ym);
if (++xm >= XTOP) {
xm = XSTART;
bc = bcorn;
ac += ygap;
if (++ym >= YTOP) return(TRUE);
} else {
bc += xgap;
}
return(FALSE);
}
/***************************
** Mandelbrot calculation **
***************************/
int Mandcolor(rl,im)
double rl, im; /* real and imaginary parts of number */
{
int ct;
double az = 0., bz = 0.;
double tm;
double sqrt();
for (ct = 0;ct < LIMIT; ++ct) {
tm = az*az - bz*bz + rl;
bz = 2*az*bz + im;
az = tm;
if (sqrt(az*az+bz*bz) > szlim) break;
}
ct = (ct >> 3) & 15;
return(ct);
}
fieldbox()
{
SetAPen(rp, 15); /* Clear the drawing area */
SetDrMd(rp, JAM1);
RectFill(rp, (XSTART-1), (YSTART-1), XTOP, YTOP);
}
span_box(xi,yi)
int xi, yi;
{
ULONG class;
USHORT code;
int x1, y1, x2, y2, xt, yt, xs, ys, xmp, ymp;
xt = xi;
yt = yi;
ac = acorn += (yi-YSTART)*ygap;
bc = bcorn += (xi-XSTART)*xgap;
xm = XSTART;
ym = YSTART;
filled = FALSE;
SetAPen(rp, 8);
SetDrMd(rp, JAM1|COMPLEMENT);
FOREVER {
if (xt > xi) {
x1 = xi;
x2 = xt;
} else {
x1 = xt;
x2 = xi;
}
if (yt > yi) {
y1 = yi;
y2 = yt;
} else {
y1 = yt;
y2 = yi;
}
RectFill(rp, x1, y1, x2, y2);
RectFill(rp, x1, y1, x2, y2);
xmp = w->MouseX;
ymp = w->MouseY;
if (OKAY) {
xt = xmp;
yt = ymp;
}
while((message = (struct IntuiMessage *)GetMsg(w->UserPort)) != NULL) {
class = message->Class;
code = message->Code;
ReplyMsg(message); /* Can't reply until done using it! */
if(class == CLOSEWINDOW) { /* Exit the program */
CloseWindow(w);
CloseScreen(screen);
CloseLibrary(IntuitionBase);
CloseLibrary(GfxBase);
exit();
};
if (OKAY) {
if(class == MOUSEBUTTONS && code == SELECTDOWN) {
xs = xt-xi;
ys = yt-yi;
xside = ((xs==0)?.5:xs)*xgap;
yside = ((ys==0)?.5:xs)*ygap;
xgap = xside/XPIXSIDE;
ygap = yside/YPIXSIDE;
fieldbox();
return(0);
}
}
}
}
}
/***************** eof *****************************/